package com.yuke.cloud.common.security.auth.app.authentication.openid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.UsersConnectionRepository;

/**
 * The class Open id authentication provider.
 *
 * @author
 */
@Slf4j
public class OpenIdAuthenticationProvider implements AuthenticationProvider {

	//mod by wg 20180928 使openid登录验证与普通验证流程一样
	//private SocialUserDetailsService userDetailsService;
	//private UserDetailsService userDetailsService;

	private UserInfoService userInfoService;
	private UsersConnectionRepository usersConnectionRepository;

	/**
	 * Authenticate authentication.
	 *
	 * @param authentication the authentication
	 *
	 * @return the authentication
	 *
	 * @throws AuthenticationException the authentication exception
	 */
	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		log.info("OpenIdAuthenticationProvider->Authentication authenticate={}",authentication);
		OpenIdAuthenticationToken authenticationToken = (OpenIdAuthenticationToken) authentication;
		log.info("OpenIdAuthenticationProvider->authenticate->OpenIdAuthenticationToken authenticationToken={}",authenticationToken);
//		Set<String> providerUserIds = new HashSet<>();
//		providerUserIds.add((String) authenticationToken.getPrincipal());
//		Set<String> userIds = usersConnectionRepository.findUserIdsConnectedTo(authenticationToken.getProviderId(), providerUserIds);
//
//		if (CollectionUtils.isEmpty(userIds) || userIds.size() != 1) {
//			throw new InternalAuthenticationServiceException("无法获取用户信息");
//		}
		String openId = authenticationToken.getPrincipal().toString();
		System.out.println("openId="+openId);
		UserDetails user = userInfoService.findUserByOpenId(openId);
//		String userId = userIds.iterator().next();

		//mod by wg 20180928 使openid登录验证与普通验证流程一样
//		UserDetails user = userDetailsService.loadUserByUsername(userId);
			log.info("Authentication authenticate={}",authentication);
		if (user == null) {
			throw new InternalAuthenticationServiceException("无法获取用户信息");
		}

		OpenIdAuthenticationToken authenticationResult = new OpenIdAuthenticationToken(user, user.getAuthorities());

		authenticationResult.setDetails(authenticationToken.getDetails());

		return authenticationResult;
	}

	/**
	 * Supports boolean.
	 *
	 * @param authentication the authenticationf
	 *
	 * @return the boolean
	 */
	@Override
	public boolean supports(Class<?> authentication) {
		return OpenIdAuthenticationToken.class.isAssignableFrom(authentication);
	}

	/**
	 * Gets user details service.
	 *
	 * @return the user details service
	 */
	//mod by wg 20180928 使openid登录验证与普通验证流程一样
//	public SocialUserDetailsService getUserDetailsService() {
//		return userDetailsService;
//	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	/**
	 * Sets user details service.
	 *
	 * @param userInfoService the user details service
	 */
	//mod by wg 20180928 使openid登录验证与普通验证流程一样
//	public void setUserDetailsService(SocialUserDetailsService userDetailsService) {
//		this.userDetailsService = userDetailsService;
//	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * Gets users connection repository.
	 *
	 * @return the users connection repository
	 */
	public UsersConnectionRepository getUsersConnectionRepository() {
		return usersConnectionRepository;
	}

	/**
	 * Sets users connection repository.
	 *
	 * @param usersConnectionRepository the users connection repository
	 */
	public void setUsersConnectionRepository(UsersConnectionRepository usersConnectionRepository) {
		this.usersConnectionRepository = usersConnectionRepository;
	}

}
